# -*- coding: utf-8 -*-

'''eof
name:借款人（自然人）年龄校验
code:R01_IG_AgeCheck_CF
tableName:
columnName:
groups:个人业务信息校验
dependencies:CF_CQ_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


import sys
import pandas as pd
import json
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def get_age(str1):
    try:
        date1 = datetime.now()
        date2 = date_format(str1)
        diff_year = date1.year - date2.year
        diff_month = date1.month - date2.month
        diff_day = date1.day - date2.day
        age = diff_year + diff_month/12.0 + diff_day/365.0
        return age
    except:
        return u'缺失值'


# 统一转成datetime对象，只能转年月日的字符串
def date_format(time):
    import re
    r = re.compile(r'\D+')
    try:
        date = datetime.strptime(time, '%Y.%m.%d')
    except:
        try:
            date = datetime.strptime(time, '%Y-%m-%d')
        except:
            try:
                date = datetime.strptime(time, '%Y/%m/%d')
            except:
                try:
                    tmp = r.sub('', time)
                    date = datetime.strptime(tmp, '%Y%m%d')
                except:
                    date = None
    return date


def float_check(target):
    try:
        float(target)
        return True
    except:
        return False


def getRule01():
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    card_id = ratingRequest.get("legalPersonIdCard")
    time_limit = ratingRequest.get("timeLimit")
    if card_id not in null_type_list and len(card_id) == 18 and float_check(time_limit):
        factor = get_age(card_id[6:14])
        factor1 = factor + float(time_limit)/12
        if factor <= 23 or factor1 > 60:
            return 0
        else:
            return 1
    else:
        raise ValueError("请求参数（身份证号，申请期限）异常;身份证号=" + card_id + ";申请期限=" + time_limit)


if __name__ == "__main__":
    ratingRequest = {"legalPersonIdCard":"50010120080292411","timeLimit":"12","bb":""}
    aa = getRule01()
    print aa


result = getRule01()